package huanyuxingcheng.nonameinstallhelper;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

public class MainActivity extends BaseActivity implements View.OnClickListener {

    /**
     * 无名杀玄武安装器
     */
    private TextView title;
    /**
     * 无名杀本体安装
     */
    private Button mainInstall;
    /**
     * 扩展安装
     */
    private Button extensionInstall;
    /**
     * 十周年UI安装
     */
    private Button decadeInstall;
    /**
     * 无名杀精品武侠扩展《玄武江湖》安装
     */
    private Button xuanwuInstall;
    private LinearLayout mainButtons;
    /**
     * 检测中
     */
    private TextView mainStatusText;

    private Button decadeCardInstall;

    private Button skinInstall;

    private Button bigSkinInstall;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        if (Build.VERSION.SDK_INT > 22) {
            int ret = checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE);
            if (ret == PackageManager.PERMISSION_GRANTED) {
                afterPermissionGranted();
            } else {
                requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 100);
            }
        } else {
            afterPermissionGranted();
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
            afterPermissionGranted();
        } else {
            afterPermissionReject();
        }
    }

    private void afterPermissionReject() {
        mainStatusText.setText("没有所需要的权限！");
    }

    private void afterPermissionGranted() {
        StringBuilder sb = new StringBuilder();
        sb.append("QQ文件夹：");
        if(NonameUtils.qqDownloadDirectoryExist()){
            sb.append("正常，可检测到");
        }else {
            sb.append("未检测到，可能未安装QQ");
        }
        sb.append("\n");
        sb.append("无名杀（由理版）文件夹：");
        if(NonameUtils.nonameDirectoryExist()){
            sb.append("正常，可检测到");
        }else{
            sb.append("未检测到，请安装【无名杀_Android（由理版）.apk】，并至少打开一次应用！");
        }
        mainStatusText.setText(sb.toString());
    }


    private void initView() {
        title = findViewById(R.id.title);
        mainInstall = findViewById(R.id.main_install);
        mainInstall.setOnClickListener(this);
        extensionInstall = findViewById(R.id.extension_install);
        extensionInstall.setOnClickListener(this);
        decadeInstall = findViewById(R.id.decade_install);
        decadeInstall.setOnClickListener(this);
        xuanwuInstall = findViewById(R.id.xuanwu_install);
        xuanwuInstall.setOnClickListener(this);
        xuanwuInstall.setTextColor(Color.RED);
        mainButtons = findViewById(R.id.main_buttons);
        mainStatusText = findViewById(R.id.main_status_text);
        decadeCardInstall = findViewById(R.id.decade_card_install);
        skinInstall = findViewById(R.id.skin_install);
        decadeCardInstall.setOnClickListener(this);
        skinInstall.setOnClickListener(this);
        bigSkinInstall = findViewById(R.id.big_skin_install);
        bigSkinInstall.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            default:
                break;
            case R.id.main_install:
                if(checkInstallPrepared()){
                    Intent intent = new Intent(this,MainInstallActivity.class);
                    startActivity(intent);
                }
                break;
            case R.id.extension_install:
                if(checkInstallPrepared()){
                    Intent intent = new Intent(this,ExtensionActivity.class);
                    startActivity(intent);
                }
                break;
            case R.id.decade_install:
                if(checkInstallPrepared()){
                    showShortToast("请在群中下载十周年UI并按照此步骤安装！");
                    Intent intent = new Intent(this,ExtensionActivity.class);
                    startActivity(intent);
                }
                break;
            case R.id.xuanwu_install:
                if(checkInstallPrepared()){
                    Intent intent = new Intent(this,XuanwuActivity.class);
                    startActivity(intent);
                }
                break;
            case R.id.skin_install:
                if(checkInstallPrepared()){
                    Intent intent = new Intent(this,SkinInstallActivity.class);
                    startActivity(intent);
                }
                break;
            case R.id.decade_card_install:
                if(checkInstallPrepared()){
                    Intent intent = new Intent(this,DecadeCardInstallActivity.class);
                    startActivity(intent);
                }
                break;

            case R.id.big_skin_install:
                if(checkInstallPrepared()){
                    Intent intent = new Intent(this,BigSkinAudioInstallActivity.class);
                    startActivity(intent);
                }
        }
    }

    private boolean checkInstallPrepared(){
        boolean ret =  NonameUtils.nonameDirectoryExist() && NonameUtils.qqDownloadDirectoryExist();
        if(!ret){
            showShortToast("无名杀或QQ未检测到文件夹！无法进行下一步。");
        }
        return ret;
    }
}
